package com.zzld.modules.sys.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.common.dto.EasyuiTree;
import com.zzld.modules.sys.entity.Dict;
import com.zzld.modules.sys.service.IDictService;

/**
 * 资源类
 * 
 * @author zzld
 */
@Controller
@RequestMapping(value = "/sys/dict")
public class DictController {

	@Autowired
	private IDictService dictService;

	private String listurl = "/modules/sys/dict/dictList";

	@RequestMapping(method = RequestMethod.GET)
	public String res(Model model) {
		return listurl;
	}

	@RequestMapping("/getDetailById")
	@ResponseBody
	public Dict getDetailById(Model model, HttpServletRequest request) {
		Dict entity = new Dict();
		String id = request.getParameter("id");
		if (StringUtils.isNotBlank(id)) {
			try {
				entity = dictService.selectByPrimaryKey(id);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return entity;
	}

	/**
	 * 新增/修改操作
	 * 
	 * @param model
	 * @param entity
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public CommonJSON update(Model model, Dict entity) {
		CommonJSON resultJson = new CommonJSON();
		if (StringUtils.isBlank(entity.getId())) {// 新增
			entity.setId(UuidUtil.uuid().toString());
			try {
				dictService.save(entity);
				resultJson.setSuccess(true);
				resultJson.setMessage(MessageUtils.ADD_SUCCESS);
			} catch (Exception e) {
				e.printStackTrace();
				resultJson.setSuccess(false);
				resultJson.setMessage(MessageUtils.ADD_FAIL);
			}

		} else {
			try {
				dictService.updateByPrimaryKeySelective(entity);
				resultJson.setSuccess(true);
				resultJson.setMessage(MessageUtils.ADD_SUCCESS);
			} catch (Exception e) {
				e.printStackTrace();
				resultJson.setSuccess(false);
				resultJson.setMessage(MessageUtils.EDIT_WARING);
			}

		}
		return resultJson;
	}

	/**
	 * 删除操作
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public CommonJSON delete(HttpServletRequest request) {
		CommonJSON resultJson = new CommonJSON();
		String id = request.getParameter("id");
		try {
			dictService.deleteByPrimaryKey(id);
			resultJson.setSuccess(true);
			resultJson.setMessage(MessageUtils.DELETE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			resultJson.setSuccess(true);
			resultJson.setMessage(MessageUtils.DELETE_WARNING);
		}
		// 执行删除
		return resultJson;
	}

	/**
	 * 获取全部树集合
	 * 
	 * @return
	 */
	@RequestMapping("/tree")
	@ResponseBody
	public List<EasyuiTree> getAllree() {
		List<EasyuiTree> nodes = new ArrayList<EasyuiTree>();
		List<Dict> dict_list = dictService.getAll();
		if (dict_list != null && dict_list.size() > 0) {
			for (Dict dict : dict_list) {
				EasyuiTree easyuiTree = new EasyuiTree();
				easyuiTree.setId(dict.getId());
				easyuiTree.setPid(dict.getPid());
				easyuiTree.setText(dict.getName());
				nodes.add(easyuiTree);
			}
		}
		return nodes;
	}
}
